/**
***********************************************************************
*
* Copyright 2011 Stefan Wöhle
*
* This file is part of PixelMath
*
* PixelMath is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PixelMath is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PixelMath. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************
*/

#include "localizedfunctionwidget.h"
#include "emptyformulawidget.h"
#include <QLabel>

LocalizedFunctionWidget::LocalizedFunctionWidget(LocalizedFunction *formula, QWidget *parent) :
    FormulaWidget(parent)
{
    /* Appearance
     ***********************************
     *  <Label>(<Formula>,<Formula>)  *
     ***********************************/

    this->formula = formula;
    formula->setParent(this);

    centralLayout.addWidget(new QLabel(formula->label() + "(", this));

    EmptyFormulaWidget *empty;
    empty = new EmptyFormulaWidget(this);
    connect(empty, SIGNAL(formulaChoosen(FormulaWidget*)),
            this, SLOT(formulaChoosen(FormulaWidget*)));
    xWidget = empty;
    centralLayout.addWidget(xWidget);

    centralLayout.addWidget(new QLabel(",", this));

    empty = new EmptyFormulaWidget(this);
    connect(empty, SIGNAL(formulaChoosen(FormulaWidget*)),
            this, SLOT(formulaChoosen(FormulaWidget*)));
    yWidget = empty;
    centralLayout.addWidget(yWidget);

    centralLayout.addWidget(new QLabel(")", this));
}

/*********************************************************
 *
 *  formulaChoosen
 *
 *  Caught signal formulaChoosen from an EmptyFormulaWidget.
 *  Replace this widget by the signal-parameter and set the
 *  formula as your subformula.
 *
 **********************************************************/
void LocalizedFunctionWidget::formulaChoosen(FormulaWidget *formulaWidget)
{
    FormulaWidget* source = (FormulaWidget*)sender();
    centralLayout.removeWidget(source);
    source->hide();
    if (source == xWidget)
    {
        xWidget = formulaWidget;
        formulaWidget->setParent(this);
        ((LocalizedFunction*)formula)->setX(&formulaWidget->formulaElement());        
        connect(xWidget, SIGNAL(destroyed(QObject*)),
                this, SLOT(replaceSubwidget(QObject*)));
        centralLayout.insertWidget(1, xWidget);
    }
    else if (source == yWidget)
    {
        yWidget = formulaWidget;
        formulaWidget->setParent(this);
        ((LocalizedFunction*)formula)->setY(&formulaWidget->formulaElement());        
        connect(yWidget, SIGNAL(destroyed(QObject*)),
                this, SLOT(replaceSubwidget(QObject*)));
        centralLayout.insertWidget(3, yWidget);
    }
    //delete source;
}

void LocalizedFunctionWidget::replaceSubwidget(QObject *destroyed)
{
    EmptyFormulaWidget *empty;
    empty = new EmptyFormulaWidget(this);
    connect(empty, SIGNAL(formulaChoosen(FormulaWidget*)),
            this, SLOT(formulaChoosen(FormulaWidget*)));

    if (destroyed == xWidget)
    {
        xWidget = empty;
        ((LocalizedFunction*)formula)->setX(NULL);
        centralLayout.insertWidget(1, xWidget);
    }
    else if (destroyed == yWidget)
    {
        yWidget = empty;
        ((LocalizedFunction*)formula)->setY(NULL);
        centralLayout.insertWidget(3, yWidget);
    }
}
